<template>
	<view class="goods-item">
		<!-- 商品左侧图片区域 -->
		<view class="goods-item-left">
			<radio class="showRadio" :checked='goods.selected' color="#27BA9B" @click="changeSelected"></radio>
			<image :src="goods.picture" class="goods-pic"></image>
		</view>
		<!-- 商品右侧信息区域 -->
		<view class="goods-item-right">
			<view @click="gotoDetail">
				<!-- 商品标题 -->
				<view class="name ellipsis">{{goods.name}}</view>
				<view class="type ellipsis">{{goods.attrsText }}</view>
			</view>
			<view class="goods-info-box">
				<!-- 商品价格 -->
				<view class="goods-price">￥{{goods.price | tofixed}}</view>
				<!-- 商品数量 -->
				<!-- 数量最小值:min="1" 已经修复 -->
				<!-- change事件 -->
				<uni-number-box :min="1" :max="goods.stock" @change="changeCount" @blur="changeCount"
					:value="goods.count"></uni-number-box>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		name: "my-goods",
		// 定义 props 属性，用来接收外界传递到当前组件的数据
		props: {
			// 商品的信息对象
			goods: {
				type: Object,
				defaul: () => {},
			}
		},
		data() {
			return {};
		},
		filters: {
			// 把数字处理为带两位小数点的数字
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		},
		onLoad() {

		},
		methods: {
			// 点击后状态取反
			changeSelected() {
				this.$emit('updateSelected', this.goods.stock)
			},
			// 输入和修改数量的时候数据
			changeCount(value) {
				// console.log(e.detail.value);
				// 如果输入不是数字则返回1
				const count = Number(value) || 1
				// 将输入的值传父
				this.$emit('updateCount', {
					count,
					skuId: this.goods.skuId,
					selected: this.goods.selected
				})
			},
			gotoDetail() {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?id=' + this.goods.id
				})
			}
		}
	}
</script>

<style lang="scss">
	.ellipsis {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.goods-item {
		display: flex;
		padding: 20rpx 10rpx;
		border-bottom: 2rpx solid #f0f0f0;
		background-color: #fff;

		.goods-item-left {
			margin-right: 10rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.goods-pic {
				width: 170rpx;
				height: 170rpx;
				display: block;
			}
		}

		.showRadio {}

		.goods-item-right {
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: space-between;

			.name {
				height: 72rpx;
				font-size: 26rpx;
				color: #444;
			}

			.type {
				line-height: 1.8;
				padding: 0 15rpx;
				font-size: 24rpx;
				align-self: flex-start;
				border-radius: 4rpx;
				color: #888;
				background-color: #f7f7f8;
			}

			.goods-info-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.goods-price {
				line-height: 1;
				font-size: 26rpx;
				color: #444;
				margin-bottom: 2rpx;
			}
		}
	}
</style>